package mechanics;


import java.util.Scanner;
	

/**
 * class of representing the main table in poker game 
 * @author wiktor
 *
 */
public class Table {
	Player player[];
	Hand playerHand[];
	int playerQuantity;
	int victory[];
	Deck deck;
	Human human[];
	Bot bot[];
	Hand humanHand[];
	Hand botHand[];
	int botQuantity;
	int humanQuantity;
	
	Scanner scan = new Scanner(System.in);
	
		Table(int humanQuantity, int botQuantity) {
			playerQuantity = humanQuantity + botQuantity; 
		
			if(playerQuantity > 4 || playerQuantity < 2)
				System.out.println("Invalid number of players");
			
				//throw new IllegalArgumentException(" wrong player quantity ");
		
		
			deck = new Deck();
			
			this.humanQuantity = humanQuantity;
			human = new Human[humanQuantity];
			humanHand = new Hand[humanQuantity];
			
			
			this.botQuantity = botQuantity;
			bot = new Bot[botQuantity];
			botHand = new Hand[botQuantity];
			
			for(int i = 0; i < humanQuantity; i++){
				
				humanHand[i] = new Hand( deck );
				System.out.println("write name of player " + (i+1));
				String name = scan.next();
				human[i] = new Human(humanHand[i], name);
			}
			
			for(int i = 0; i < botQuantity; i++){
				botHand[i] = new Hand ( deck );
				System.out.println("write name of bot " + (i+1));
				String botName = scan.next();
				bot[i] = new Bot(botHand[i], botName );
			}
			
			player = new Player[playerQuantity];
			int playerCount;
			for( playerCount = 0; playerCount < humanQuantity; playerCount++){
				 player[playerCount] = human[playerCount];
			}
			for( playerCount = humanQuantity; playerCount < playerQuantity; playerCount++){
				player[playerCount] = bot[playerCount-humanQuantity];
			}
			
			playerHand = new Hand[playerQuantity];
			for( playerCount = 0; playerCount < humanQuantity; playerCount++){
				 playerHand[playerCount] = humanHand[playerCount];
			}
			for( playerCount = humanQuantity; playerCount < playerQuantity; playerCount++){
				playerHand[playerCount] = botHand[playerCount - humanQuantity];
			}
			
			
		}
		
		void Discard(Player player, Hand hand){ 
			if(hand.Swap){
				if (player.bot)
					botDiscard((Bot) player, hand);
				else{
				
				System.out.println("select amount cards to change ");
				int cardAmount = scan.nextInt();
				if(cardAmount > 4 || cardAmount < 0){
					System.out.println("invalid amount of cards to discard");
					Discard(player, hand);
				}
				int discard[] = new int [cardAmount];
				
				for(int i = 0; i < cardAmount; i++){
					System.out.println( " select card to discard ");
					discard[i] = scan.nextInt();
					if(discard[i] > 4 || discard[i] < 0){
						System.out.println("invalid card number");
						Discard(player, hand);
					}
				}
				for(int i = 0; i < discard.length; i++){
					for(int j = i+1; j < discard.length; j++){
						if(discard[i] == discard[j]){
							System.out.println("you cannot change this the same card multiple times!");
							Discard (player, hand);
						}
					}
				}
				
				
				for(int i = 0; i < discard.length; i++){
					
					deck.returnToDeck(hand.cards[discard[i]]);
					deck.Shuffle();
					hand.cards[discard[i]] = deck.drawFromDeck();
					hand.valuation();
				
				}
				hand.Swap = false;
			}
			}
		}
		private void botDiscard(Bot bot, Hand hand) {
				
				int cardAmount = bot.generator.nextInt(5);
				int discard[] = new int [cardAmount];
				
				for(int i = 0; i < cardAmount; i++){
					
					discard[i] = bot.generator.nextInt(5);
					}
				
				for(int i = 0; i < discard.length; i++){
					for(int j = i+1; j < discard.length; j++){
						while(discard[i] == discard[j]){
							discard[j] = bot.generator.nextInt(5);
						}
					}
				}
				for(int i = 0; i < discard.length; i++){
					
					deck.returnToDeck(hand.cards[discard[i]]);
					deck.Shuffle();
					hand.cards[discard[i]] = deck.drawFromDeck();
					hand.valuation();
				
				}
				hand.Swap = false;
		}

		void Gameplay(){
			
			int max = -4;
			String winner = "init";
			
			victory = new int [playerQuantity];
			
			for(int i = 0; i < playerQuantity; i++){
			
				if(playerHand[i].Swap){
					System.out.println("turn of " + player[i].name);
					playerHand[i].display();
					playerHand[i].displayAll();
					Discard(player[i], playerHand[i]);
					System.out.println(" after change ");
				    playerHand[i].displayAll();
				    playerHand[i].display();
				}
				
				for( int j = i; j < playerQuantity; j++)
					victory[i] += playerHand[i].compareTo(playerHand[j]);
				
			}
			
			int x=-1;
			for( int i = 0; i < playerQuantity; i++){
				
				if( max < victory[i]){
					
					max = victory[i];
					winner = player[i].name;
					x = i;
				}
			}
			for(int j = 0; j < playerQuantity; j++ ){
				
				if(max == victory[j] && j!=x){
					winner += ("&");
					winner +=(player[j].name);
				}
			}
			System.out.println("winner is player " + winner);
			
		}
		
		

		
}